// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword:'',
    historySearch:[]
  },
  getShoplist:function(){
    let data={keyword:this.data.keyword};
    app.requestJs('Goods/goodsList',{data}).then((res)=>{
      if(res.errcode==1){
        let resData=res.data || [];
        this.setData({
          list:resData,
        })
        
      }else app.utilJs.toast({ title: res.message });
    })
  },
  confirmSearch:function(){
    let that=this;
    let keyword=this.data.keyword;
    wx.navigateTo({
      url: '/pages/shop/shoplist/shoplist?keyword='+keyword,
      success:function(e){
        that.history_search(keyword);
      }
    })
  },
  bindkeyword:function(e){
    let value=e.detail.value;
    this.setData({
      keyword:value
    })
  },
  history_search:function(keyword){//添加历史 搜索
    let keyword_len=keyword.replace(/\s+/g,"");
    if(keyword_len.length==0)return;
    console.log(this.data.historySearch);
    let historySearch=this.data.historySearch;
    historySearch.unshift(keyword);
    historySearch=[...new Set(historySearch)].slice(0,9);
    this.setData({
      historySearch
    })
    wx.setStorageSync('historySearch',historySearch);
  },
  //清理历史
  historyClear:function(){
    let _this=this;
    if(this.data.historySearch.length==0){
      app.utilJs.toast({ title:'没有要清空历史记录' });
      return
    }
    wx.showModal({
      title: '提示',
      content: '是否清空所有搜索记录？',
      success (res) {
        if (res.confirm) {
          wx.removeStorageSync('historySearch');
          _this.setData({historySearch:[]});
        } else if (res.cancel) {
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let historySearch=wx.getStorageSync('historySearch') || [];
    this.setData({
      historySearch
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})